<?php
namespace app\admin\model;
use think\Model;
use think\exception\Jump;
class Point extends Model {
    
    public static  function pay_type($pay_type=''){
        $pay_type = array(
			0 => '每日登录',
			1 => '评论奖励',
			2 => '投稿奖励',
			3 => '会员充值',
			4 => '后台充值',
			5 => '积分兑换',
			6 => '积分购买',
			7 => '阅读收费',
			8 => '下载收费',
			9 => '发表帖子',
			10 => '每日签到',
			11 => '投稿收费',
			12 => '其他'
		);
		return $pay_type;
        
    }
		
	
		
	public static function point_add($type, $value, $pay_type, $userid, $username, $experience = 0, $remarks = '', $adminnote = '', $mod_experience = true) {
		$data = array();
		$data['trade_sn'] = create_tradenum();
		$data['money'] = $value;
		$data['userid'] = $userid;
		$data['username'] = $username;
		$data['type'] = $type;
		$data['msg'] =  self::pay_type()[$pay_type];
		$data['remarks'] = htmlspecialchars($remarks);
		$data['creat_time'] = time();
		$data['ip'] = getip();
		$data['adminnote'] = $adminnote;
		$data['status'] = 1;
		$update = $type == '1' ? 'point': 'amount';
		if($mod_experience) {
			D('member')->where('userid', $userid)->setInc('experience', $value);
			if(!$experience) $experience = D('member')->where(array('userid' => $userid))->value('experience');
			self::_check_update_group($value, $experience, $userid);
		}
		D('member')->where('userid', $userid)->setInc(''.$update.'', $value);
		D('pay')->strict(false)->insert($data);
	}
	
	public static function point_spend($type, $value, $pay_type, $userid = '', $username = '', $remarks = '') {
		if($value == 0) return false;
		if(!$userid || !$username || $value<0) Jump::error("信息不足!");
		$data = D('member')->field('point,amount')->where('userid',$userid)->find();
		if($type == '1') {
			if(($data['point']-$value)<0) Jump::error("积分不足本次交易!");
			$update = 'point';
		} else {
			if(($data['amount']-$value)<0) Jump::error("余额不足本次交易!");
			$update = 'amount';
		}
		$data = array();
		$data['trade_sn'] = create_tradenum();
		$data['money'] = $value;
		$data['userid'] = $userid;
		$data['username'] = $username;
		$data['type'] = $type;
		$data['msg'] =  self::pay_type()[$pay_type];
		$data['remarks'] = htmlspecialchars($remarks);
		$data['creat_time'] = time();
		$data['ip'] = getip();
		$affected = D('member')->where('userid', $userid)->setDec(''.$update.'', $value);
		if(!$affected) return false;
		return D('spend')->strict(false)->insert($data);
	}
	
	
	public static function _check_update_group($add, $experience, $userid) {
		$new_groupid = 1;
		$groupid = intval(Session('_groupid'));
		$data = get_group();
		if(!$data) return false;
		$exp = $experience+$add;
		foreach ($data as $k=>$v) {
			$experience_list[$k] = $v['experience'];
		}
		arsort($experience_list);
		if($exp > max($experience_list)) {
			$new_groupid = key($experience_list);
		} else {
			foreach ($experience_list as $k=>$v) {
				if($exp >= $v) {
					$new_groupid = $tmp_k;
					break;
				}
				$tmp_k = $k;
			}
		}
		if($new_groupid !== $groupid) {
			Session('_groupid',$new_groupid);
			D('member')->where('userid',$userid)->update(['groupid'=>$new_groupid]);
		}
	}
}